/**
 * Title:       FIPAContent.java
 * Copyright:   Copyright (c) 1996-2004 The Agent Factory Working Group. All rights reserved.
 * Licence:     This file is free software; you can redistribute it and/or modify
 *              it under the terms of the GNU Lesser General Public License as published by
 *              the Free Software Foundation; either version 2.1, or (at your option)
 *              any later version.
 *
 *              This file is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU Lesser General Public License for more details.
 *
 *              You should have received a copy of the GNU Lesser General Public License
 *              along with Agent Factory; see the file COPYING.  If not, write to
 *              the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *              Boston, MA 02111-1307, USA.
 */
package com.agentfactory.platform.mts;

import java.util.ArrayList;
import com.agentfactory.platform.mts.FIPAHelper;
/**
 * This class is used to contain FIPA-syntax compliant content.
 *
 * @author  Rem Collier
 */
public class FIPAContent {
    private String command;
    private ArrayList arguments;
    
    /**
     * Factory method that creates a new instance of FIPAContent based upon
     * the contents of the "content" String.
     *
     * @param content the content to be encoded in the instance.
     * @return an instance of the FIPAContent class.
     */
    public static FIPAContent newInstance(String content) {
        FIPAContent fipaContent = new FIPAContent();
        FIPAContent argument = null;
        FIPAContent value = null;
        ArrayList tokens = FIPAHelper.getMessageTokens(content.trim());
        fipaContent.command = (String) tokens.get(0);
        for (int i = 1; i < tokens.size(); i++) {
            argument = FIPAContent.newInstance((String) tokens.get(i));
            fipaContent.arguments.add(argument);
            if (argument.getCommand().equals(":name")) {
            	value = new FIPAContent();
            	value.setCommand((String) tokens.get(++i));
                fipaContent.arguments.add(value);
            }
        }
        return fipaContent;
    }
    
    /** Creates a new instance of FIPAContent */
    private FIPAContent() {
        arguments = new ArrayList();
    }
    
    /**
     * Returns the content at the specified index.
     * @param index the index to be returned.
     */
    public FIPAContent get(int index) {
        return (FIPAContent) arguments.get(index);
    }
    
    public String getString(int index) {
        return (String) arguments.get(index);
    }
    
    public String getCommand() {
        return command;
    }
    
    private void setCommand(String command) {
    	this.command = command;
    }
    
    /**
     * Converts an instance of of FIPAContent into String format.
     */
    public String toString() {
        if (arguments.size() == 0) {
            return command;
        }
        
        StringBuffer buf = new StringBuffer();
        buf.append("(").append(command).append(" ");
        boolean first = true;
        for (int i=0; i < arguments.size();i++) {
            if (first) {
                first = false;
            } else {
                buf.append(" ");
            }
            buf.append(arguments.get(i));
        }
        buf.append(")");
        return buf.toString();
    }
    
    public int size() {
        return arguments.size();
    }
    
    public static void main(String[] args) {
        FIPAContent content = FIPAContent.newInstance("(test :content (sequence \"xx s\" yy z))");
        System.out.println("Content: " + content);
        System.out.println(content.getString(1));
    }
}

